var teacherQueryUrl = basePath + "/organize/teacher/manage/query";
var teacherUpdateUrl = basePath + "/organize/teacher/manage/update";
var teacherWechatUnbindUrl = basePath + "/organize/teacher/wechat/unbind";
var teacherCreateBindEwmUrl = basePath + "/organize/teacher/create/bindEwm";

var dataGrid;
$(function() {
	loadData();
});

function loadData() {
	dataGrid = $('#teacherManageDg').datagrid({
		url : teacherQueryUrl,
		toolbar: '#tb',
		fit : true,
		fitColumns : true,
		border : true,
		pagination : true,
		idField : 'id',
		pageSize : config.pageSize,
		autoRowHeight : 140,
		pageList : config.pageList,
		nowrap : true,
		striped : true,
		rownumbers : true,
		singleSelect: true,
		fit:false,
		columns : [ [  {
			field : 'id',
			width : 20,
			align : "center",
			checkbox : true
		}, {
			field : 'action',
			title : '操作',
			resizable : true,
			align : "center",
			width : 30,
			formatter : function(value,row,index){
				var str = $.formatString('<button  type="button" class="btn btn-success btn-xs" style="margin:4px 4px;" onclick="createBindEwm(\'{0}\');">绑定</button>', row.id);
				str += $.formatString('<button  type="button" class="btn btn-danger btn-xs" style="margin:4px 4px;" onclick="wechatUnbind(\'{0}\');">解绑</button>', row.id);
				return str;
			}
		}, {
			field : 'name',
			title : '老师姓名',
			resizable : true,
			align : "center",
			width : 20
		}, {
			field : 'idNo',
			title : '身份证号',
			width : 30,
			align : "center"
		}, {
			field : 'password',
			title : '登录密码',
			width : 20,
			align : "center"
		}, {
			field : 'mobile',
			title : '手机号码',
			width : 20,
			align : "center"
		}, {
			field : 'gradeText',
			title : '年级',
			width : 20,
			align : "center"
		}, {
			field : 'departText',
			title : '部门',
			width : 40,
			align : "center"
		}, {
			field : 'position',
			title : '职位',
			resizable : true,
			align : "center",
			width : 50,
			formatter : function(value,row,index){
				return row.positionText;
			},
			editor:{
				type:'combobox',
				options:{
					valueField:'id',
					textField:'position',
					groupField:'depart',
					method:'get',
					url: basePath + "/organizeDepart/comboBox",
					multiple:true,
					editable:false,
					required:true
				}
			}
		}] ],
		onAfterEdit : function(rowIndex, rowData, changes){
			if(rowData.id){
				changes.id = rowData.id;
				addmodify(changes)
			}else {
				addmodify(rowData);
			}
		}
	});
}

function reload() {
	dataGrid.datagrid("clearSelections");
	dataGrid.datagrid('load');
}

function searchForm(formId) {
	dataGrid.datagrid('load', $.serializeObject($('#' + formId)));
}
function resetForm(formId) {
	$("#" + formId)[0].reset();
}

function save(id) {
	if(dgEditIndex[id] != null){
		$('#' + id).datagrid('endEdit', dgEditIndex[id]);
		dgEdit[id] = null;
	}
}

function addmodify(data) {
	$.ajax({
	   type: "POST",
	   url: teacherUpdateUrl,
	   data: data,
	   success: function(res){
		   if (res.code == '000'){
			   reload();
			   $.messager.alert('提示', '变更成功', 'info');
		   }else{
			   $.messager.alert('提示', '变更失败', 'error');
			   errorEdit("teacherManageDg");
		   }
		}
	});
}
function wechatUnbind(id) {
	$.ajax({
	   type: "POST",
	   url: teacherWechatUnbindUrl,
	   data: {
		   teacherId: id
	   },
	   success: function(res){
		   if (res.code == '000'){
			   $.messager.alert('提示', '解绑成功', 'info');
		   }else{
			   $.messager.alert('提示', '解绑失败', 'error');
		   }
		}
	});
}

function createBindEwm(id) {
	$('#bindForm').dialog({
		title : '二维码绑定',
		modal : true,
		width : 300,
		top : 100,
		draggable : true,
		resizable : true,
		buttons : '#btns',
		onClose : function() {
			$("#ewm").attr("src", "");
		}
	}).show();
	$.ajax({
	   type: "POST",
	   url: teacherCreateBindEwmUrl,
	   data: {
		   teacherId: id
	   },
	   success: function(res){
		   if (res.code == '000'){
			   $("#ewm").attr("src", res.data);
		   }else{
			   $.messager.alert('提示', data.msg, 'error');
		   }
		}
	});
}

